package com.jiaofamily.localad.sdk.adapters;

import net.youmi.android.AdListener;
import net.youmi.android.AdManager;
import net.youmi.android.AdView;
import android.app.Activity;
import android.graphics.Color;
import android.text.GetChars;
import android.util.Log;

import com.jiaofamily.localad.sdk.LocalAdLayout;
import com.jiaofamily.localad.sdk.obj.Extra;
import com.jiaofamily.localad.sdk.obj.Ration;
import com.jiaofamily.localad.sdk.util.LocalAdUtil;

public class YoumiAdapter extends LocalAdAdapter implements AdListener{
	private static final String TAG=YoumiAdapter.class.getSimpleName();
	
	private AdView adView=null;
	
	public YoumiAdapter(LocalAdLayout layout, Ration ration) {
		super(layout, ration);
		// TODO Auto-generated constructor stub
		AdManager.init(ration.key,ration.key2,layout.extra.cycleTime,false,2.1);
	}

	@Override
	public void handle() {
		// TODO Auto-generated method stub
		Log.d(TAG,"Enter into Youmi");
		LocalAdLayout layout=this.layout.get();
		if(layout==null){
			return;
		}
		Extra extra=layout.extra;
		int bgColor=Color.rgb(extra.bgRed, extra.bgGreen, extra.bgBlue);
		int fgColor=Color.rgb(extra.fgRed, extra.fgGreen, extra.fgBlue);
		
		Activity activity=layout.activityReference.get();
		if(activity==null){
			return;
		}
		adView=new AdView(activity,bgColor,fgColor,255);
		adView.setHorizontalScrollBarEnabled(false);
		adView.setVerticalScrollBarEnabled(false);
		adView.setAdListener(this);
		layout.manager.resetRollover();

		layout.handler.post(new LocalAdLayout.ViewAdRunnable(layout, adView));
		layout.rotateThreadedDelayed();
		
	}

	public void onConnectFailed(AdView arg0) {
		// TODO Auto-generated method stub
		Log.d(TAG,"Youmi onConnectionFailed");
		arg0.setAdListener(null);
		
		LocalAdLayout layout=this.layout.get();
		if(layout==null){
			return;
		}
		//reduce weight
		if(LocalAdLayout.isDynamicAdjustWeight) {
			layout.manager.subWeight(LocalAdUtil.NETWORK_TYPE_YOUMI);
		}
		
		layout.manager.resetRollover_pri();
		layout.rollover_pri();
	}

	public void onReceiveAd(AdView arg0) {
		// TODO Auto-generated method stub
		Log.d(TAG,"Youmi onReceiveAd");
		Count.countShow(this.layout.get().getContext(),this.layout.get().activeRation);
//		LocalAdLayout layout=this.layout.get();
//		if(layout==null){
//			return;
//		}
		/*call here will prevent ad blank for some seconds as internet transfer time*/
//		layout.isReceiveAdTimeOut=false;
//		layout.manager.resetRollover();
//		if(this.adView!=null){
//			layout.handler.post(new LocalAdLayout.ViewAdRunnable(layout, this.adView));
//		}
//		layout.rotateThreadedDelayed();
	}
}
